package com.zfzs.post.rest;

import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.utils.ElAdminConstant;
import com.zfzs.post.modules.domain.VerificationCode;
import com.zfzs.post.modules.domain.vo.EmailVo;
import com.zfzs.post.service.EmailService;
import com.zfzs.post.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;

/**
 * @author ck
 * @date 2018-12-26
 */
@RestController
@RequestMapping("api")
public class VerificationCodeController {

    @Autowired
    private VerificationCodeService verificationCodeService;

    @Autowired
    @Qualifier("jwtUserDetailsService")
    private UserDetailsService userDetailsService;

    @Autowired
    private EmailService emailService;

    @PostMapping(value = "/code/resetEmail")
    public ResponseResult resetEmail(@RequestBody VerificationCode code) throws Exception {
        code.setScenes(ElAdminConstant.RESET_MAIL);
        EmailVo emailVo = verificationCodeService.sendEmail(code);
        emailService.send(emailVo, emailService.find());
        return ResponseResult.ok();
    }

    @PostMapping(value = "/code/email/resetPass")
    public ResponseResult resetPass(@RequestParam String email) throws Exception {
        VerificationCode code = new VerificationCode();
        code.setType("email");
        code.setValue(email);
        code.setScenes(ElAdminConstant.RESET_MAIL);
        EmailVo emailVo = verificationCodeService.sendEmail(code);
        emailService.send(emailVo, emailService.find());
        return ResponseResult.ok();
    }

    @GetMapping(value = "/code/validated")
    public ResponseResult validated(VerificationCode code) {
        verificationCodeService.validated(code);
        return ResponseResult.ok();
    }
}
